<?php


namespace App\Http\Controllers\Api\V1\Front;


use App\Http\Controllers\Api\V1\Manager\Mall\CategoryController;
use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\CheckRecord;
use App\Models\Record;
use App\Models\RecordType;
use App\Models\User;
use Illuminate\Http\Request;

class RecordController extends Controller
{
    public function index(Request $request)
    {
        $userId = $request->USERID;
        $recordTypeId = $request->recordTypeId;
        $user = User::find($userId);
        if(!$user['idCard'] || !$user['userName']){
             return $this->apiResponse('', config('errorCode.NOT_IDCARD'));
        }
        $data = Record::select('t1.*', 't2.categoryName')
            ->from((new Record())->getTable() . ' as t1')
            ->join((new Category())->getTable() . ' as t2', 't1.categoryId', '=', 't2.categoryId')
            ->where('t2.recordTypeId', $recordTypeId)
            ->where(['t1.userName' => $user['userName'], 't1.idCard' => $user['idCard'],'t1.status'=>1,'t1.phone'=>$user['phone']])
            ->orderByDesc('t1.createTime')->get()->toArray();
        return $this->apiResponse($data);
    }
    public function recordList(Request $request)
    {
        $recordId = $request->recordId;
        $record = Record::find($recordId);
        $data = Category::where('parentId',$record['categoryId'])->orderBy('sort','asc')->get()->toArray();
        return $this->apiResponse($data);
    }

    public function recordDetail(Request $request)
    {
        $recordId = $request->recordId;
        $categoryId = $request->categoryId;
        $data = CheckRecord::select()->where('recordId',$recordId)->where('categoryId',$categoryId)->orderBy('sort')->get()->toArray();
        return $this->apiResponse($data);
    }

    public function recordType(Request $request)
    {
        $data = RecordType::select()->orderBy('sort')->get()->toArray();
        return $this->apiResponse($data);
    }
}